package com.sds.sweng.swat.miplatform.ui.design.actions;

import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.ide.IDEActionFactory;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;

import com.sds.sweng.swat.miplatform.ui.design.editors.MiplatformDesignEditor;
import com.sds.sweng.swat.miplatform.ui.design.editors.MiplatformSourceEditor;

public class MiplatformUIDesignerActionBarContributor extends MultiPageEditorActionBarContributor {

	private IEditorPart activeEditorPart;

	public MiplatformUIDesignerActionBarContributor() {
		super();
	}

	public void setActivePage(IEditorPart part) {
		if (activeEditorPart == part)
			return;

		activeEditorPart = part;

		IActionBars actionBars = getActionBars();
		actionBars.clearGlobalActionHandlers();

		if (actionBars != null) {
			if (part instanceof MiplatformDesignEditor) {
				MiplatformDesignEditor editor = (MiplatformDesignEditor) part;

				actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), editor.getAction(IWorkbenchActionConstants.UNDO));
				actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), editor.getAction(IWorkbenchActionConstants.REDO));
				actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), editor.getAction(IWorkbenchActionConstants.DELETE));
				actionBars.updateActionBars();
			} else if (part instanceof MiplatformSourceEditor) {
				MiplatformSourceEditor editor = (MiplatformSourceEditor) part;

				actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), editor.getAction(ITextEditorActionConstants.DELETE));
				actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), editor.getAction(ITextEditorActionConstants.UNDO));
				actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), editor.getAction(ITextEditorActionConstants.REDO));
				actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), editor.getAction(ITextEditorActionConstants.CUT));
				actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), editor.getAction(ITextEditorActionConstants.COPY));
				actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), editor.getAction(ITextEditorActionConstants.PASTE));
				actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), editor.getAction(ITextEditorActionConstants.SELECT_ALL));
				actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), editor.getAction(ITextEditorActionConstants.FIND));
				actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), editor.getAction(IDEActionFactory.BOOKMARK.getId()));
				actionBars.updateActionBars();
			}
		}
	}

}
